package com.xmg.boot.demo.bootlesson1.user.service.dm.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.parser.SqlInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.xmg.boot.demo.bootlesson1.user.entity.dm.Department;
import com.xmg.boot.demo.bootlesson1.user.entity.dm.DepartmentVo;
import com.xmg.boot.demo.bootlesson1.user.entity.dm.Employee;
import com.xmg.boot.demo.bootlesson1.user.entity.dto.StudentDto;
import com.xmg.boot.demo.bootlesson1.user.mapper.dm.DepartmentMapper;
import com.xmg.boot.demo.bootlesson1.user.service.dm.DepartmentService;
import com.xmg.boot.demo.bootlesson1.user.service.dm.EmployeeService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 微信搜一搜：贺贺学编程
 * @since 2023-11-29
 */
@Service
@DS("db_dm8")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private EmployeeService employeeService;


    @Override
    public List<Department> findDepartmentAll() {
        return this.baseMapper.findDepartmentAll();
    }

    @Override
    public List<Department> findByCondition(String dpName) {
        LambdaQueryWrapper<Department> queryWrapper=new LambdaQueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(dpName)){
            queryWrapper.like(Department::getDepartmentName,dpName);
        }
        return baseMapper.findByCondition(queryWrapper);
    }

    @Override
    public List<DepartmentVo> findByVo(){
        return baseMapper.findByVo();
    }

    @Override
    public IPage<Department> findDepartmentPage(IPage<Department> page) {
        //IPage<Department> result = this.page(page);
        page = this.page(page);
        IPage<Department> result=new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords());
        return result;
    }

    @Override
    @Transactional
    public boolean AddTwoTable() {

        Department department=new Department();
        department.setDepartmentName("测试部门");
        this.save(department);

        Employee employee=new Employee();
        employee.setEmployeeName("测试1");
        employee.setHireDate(new Date());
        employee.setSalary(new BigDecimal("ssss"));
        employeeService.save(employee);

        return false;
    }

    @Override
    public List<Department> selectProc(int departmentId){
        return baseMapper.selectProc(departmentId);
    }

   // @Override
//    public String getEmployeeInfo(int departmentId) {
//        return new SQL() {{
//            SELECT("*");
//            FROM("EMPLOYEE");
//            UNION ALL
//            SELECT("*");
//            FROM("department");
//        }}.toString();
//    }
}
